// send_video.c
//
// To compile:
//
// gcc -o send-video send_video.c 
//
// Run using
//
// send-video myvideofile.mpg
//


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* User Defined Variable --
 * Change this to tweak speeds for multicasting on your network
 * Lower values mean faster speeds, but a higher chance for packet loss
 * Higher values mean slower speeds, but more reliable transmission
 * 100 = 1 second
 * 50 = .5 seconds
 */
const int VIDEO_DELAY  = 100; //delay in 1/100secs between sending each sub-video (default = 100)

/* User Defined Variable --
 * Change this to adjust bitrate of the videos.
 * Leave empty string for regular compression
 * Try the following
 * " -b 64k "   for 64kbps
 * " -b 128k "  for 128kbps
 * " -b 1200k " for 1200kbps
 */
const char USER_BITRATE[25] = ""; //Default: "" (empty string)

int main(int argc, char *argv[])
{
    int 	    v, success;
    char        system_call[512];
    char	    filename[256];
    char 	    num_buf[10];
    FILE        *duration_file;
    char        duration[20];
    int 	    hours, minutes, seconds, total_time;
    char	    *ptr;

	//error handling: user must enter an argument
	if(argc != 2)
	{
		printf("You must enter 2 arguments, but you entered %d \n", argc);
		exit(1);
	}

    // calculate number of seconds in video
    
	strcpy(system_call, "ffmpeg -i ");
	strcat(system_call, argv[1]);
	strcat(system_call, " 2>&1 | grep \"Duration\" | cut -d ' ' -f 4 | sed s/,// > duration.txt");
	system(system_call);
	
	duration_file = fopen ("duration.txt", "rt");
	fgets(duration, 20, duration_file);
    fclose(duration_file);

	printf("-------------------------------\n");
	printf("Duration           :%s", duration);
	hours = ((int)duration[0]-48)*10 + (int)duration[1]-48;
    printf("Hours              :%d\n", hours);
	minutes = ((int)duration[3]-48)*10 + (int)duration[4]-48;
	printf("Minutes            :%d\n", minutes);
	seconds = ((int)duration[6]-48)*10 + (int)duration[7]-48 + (duration[9]!='0' | duration[10]!='0');
	printf("Seconds            :%d\n", seconds);
	total_time = seconds + minutes*60 + hours*3600;
	printf("Videos to generate :%d\n", total_time);
	printf("-------------------------------\n\n");

    // generate sub-videos of a second each

    for (v=0 ; v < total_time; v++)
    {
		
	strcpy(filename, argv[1]);
	ptr = strrchr(filename, '.');
	if (ptr != 0) *ptr = 0;       // remove file extension from filename
	sprintf(num_buf, "_%d", v);
        strcat(filename, num_buf);
	strcat(filename, ".mpg");

	strcpy(system_call, "ffmpeg -i ");
        strcat(system_call, argv[1]);
    strcat(system_call, USER_BITRATE); //specify user-defined bitrate
	strcat(system_call, " -an -y -v -1 -ss ");  //strip sound, no overwrite warning, low verbosity
	sprintf(num_buf, "%02d", v/3600);
        strcat(system_call, num_buf);
	strcat(system_call, ":");
	sprintf(num_buf, "%02d", v%3600/60);
        strcat(system_call, num_buf);
	strcat(system_call, ":");
	sprintf(num_buf, "%02d", v%60);
        strcat(system_call, num_buf);
	strcat(system_call, ".00 -t 00:00:01.00 tmpsplit/");
	strcat(system_call, filename);

	system_call[510] = 0;
	
	printf("%s\n",system_call);
	system(system_call);
    	
	printf("%s\n",filename);
	success = send_video(filename);
	usleep((VIDEO_DELAY*10000)); //wait X seconds after sending each sub-video
	
    }
//unix call to empty temp folder
system("rm -f tmpsplit/*");

    return 0;
}
